import { Injectable } from '@angular/core';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FrameContext } from '@farris/devkit';

@Injectable()
export class SbssLuPlanDetailFrmControllerService {
  constructor(
    private frameContext: FrameContext,
  ) {}

/**
 * 设定计划ID
 */
  addPlandId(){
    this.frameContext.uiState['rph'] = this.frameContext.bindingData.getValue(['rph'])
    this.frameContext.uiState['id'] = this.frameContext.bindingData.getValue(['id'])
  }


  /**
   * 添加子表数据
   * @remarks 
   * @returns 
   */
  addStands(): any {
    let checkedList = this.frameContext.uiState['checkedList']

    let id = this.frameContext.bindingData.getValue(['id'])

    const rep = this.frameContext.repository as BefRepository<any>;
    let datas  = []

    let nowIdList = this.frameContext.appContext.componentManager.get(['sbssLuPlanDetail-rpzy-component_sbssluplandetail_rhmx-uc8v-component','rhmx-uc8v-dataGrid']).data.map(i=>i.lustand.lustand)

    checkedList.forEach(i=>{
      let d = i.data
      
      let has = nowIdList.some(c => {
        if(d.id == c){
          return true
       }
       return false
      })


      if (!has){
        datas.push({
            lustand: {
              lustand: d.id,
              lustand_devClass: d.devClass,
              lustand_devSpec: d.devSpec,
              lustand_luMech: d.luMech,
              lustand_luItem: d.luItem,
              lustand_note: d.note,
              lustand_cycleType: d.cycleType,
              lustand_cycleLabel: d.cycleLabel,
              lustand_cycleValue: d.cycleValue,
              lustand_luMode: d.luMode,
              lustand_luFatName: d.luFatName,
              lustand_luActive:d.luActive
            }
          })
      }
    })

    let isCfg = this.frameContext.bindingData.getValue(['rhp'])
    if(!isCfg){
      this.frameContext.bindingData.setValue(['rhp'],true,true,true)
    }
    rep.batchAppendByPath('/' + id + '/rhmxs',datas).subscribe()
  }


    /**
   * 删除子表后
   * @remarks 
   * @returns 
   */
  deleteStands(){
    let lustand = this.frameContext.bindingData.getValue(['rhmxs'])
    console.log(lustand.length)
    if(lustand.length < 1){
      this.frameContext.bindingData.setValue(['rhp'],false,true,true)
    }
  }
}
